package com.xf.sys.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

import com.xf.pm.common.PageData;
import com.xf.pm.common.PageForm;
import com.xf.pm.util.MD5;
import com.xf.pm.util.PageUtils;
import com.xf.sys.entity.SysUser;
import com.xf.sys.mapper.SysUserMapper;
import com.xf.sys.service.ISysUserService;

/**
 *SysUserService.java
 *SysUserService
 *author:lb
 *date:2016年10月1日
 */
@Service
public class SysUserService implements ISysUserService {

	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Override
	public int insertSysUser(SysUser sysUser) {
		logger.info("添加用户开始");
		int result=0;
		RedisAtomicLong userId = new RedisAtomicLong("sysUserId", redisTemplate.getConnectionFactory());
		SysUser sUser=sysUserMapper.selectByLoginUser(sysUser.getLoginUser());
		if(null==sUser){
			sysUser.setUserId((int)userId.incrementAndGet());
			String loginPwd=MD5.digest(sysUser.getLoginUser()+sysUser.getLoginPwd()+"xf");
			sysUser.setLoginPwd(loginPwd);
			result=sysUserMapper.insertSysUser(sysUser);
		}
		return result;
	}

	@Override
	public int deleteSysUser(Integer[] userIds) {
		logger.info("删除用户开始");
		return sysUserMapper.deleteSysUser(userIds);
	}

	@Override
	public int updateSysUser(SysUser sysUser) {
		logger.info("修改用户开始");
		return sysUserMapper.updateSysUser(sysUser);
	}

	@Override
	public PageData selectSysUserList(PageForm form,SysUser sysUser) {
		logger.info("查询用户开始");
		PageUtils.preparePaging(form);
		List<SysUser> list = null;
		PageData listData = null;
		try {
			list = sysUserMapper.selectSysUserList(sysUser);
			listData = PageUtils.wrapPageData(list);
		} catch (Exception e) {
			logger.error("查询系統用戶数据失败!");
			e.printStackTrace();
		}
		return listData;
	}

	@Override
	public SysUser selectSysUser(Integer userId) {
		logger.info("查询用户开始");
		return sysUserMapper.selectSysUser(userId);
	}

	@Override
	public SysUser selectByLoginUser(String loginUser) {
		logger.info("查询用户开始");
		return sysUserMapper.selectByLoginUser(loginUser);
	}
	


}
